package com.unknowpupil.alibaba.cloud.oauth.integration.authenticator;

import com.unknowpupil.alibaba.cloud.oauth.integration.IntegrationAuthentication;
import com.unknowpupil.alibaba.cloud.oauth.vo.SysUserAuthentication;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * 默认登录处理
 * @author LIQIU
 * @date 2018-3-31
 **/
@Component
@Primary
public class UsernamePasswordAuthenticator extends AbstractPreparableIntegrationAuthenticator {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public SysUserAuthentication authenticate(IntegrationAuthentication integrationAuthentication) {
        SysUserAuthentication sysUserAuthentication = new SysUserAuthentication();
        sysUserAuthentication.setId(1L);
        sysUserAuthentication.setUsername("user");
        sysUserAuthentication.setPassword(passwordEncoder.encode("123"));
        sysUserAuthentication.setPhone("187");
        return sysUserAuthentication;
    }

    @Override
    public void prepare(IntegrationAuthentication integrationAuthentication) {

    }

    @Override
    public boolean support(IntegrationAuthentication integrationAuthentication) {
        return StringUtils.isEmpty(integrationAuthentication.getAuthType());
    }
}
